package ar.edu.unicen.pladema.ui.panel.options;

import java.util.Observable;
import java.util.Observer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

import ar.edu.unicen.pladema.main.SWTUtils;
import ar.edu.unicen.pladema.ui.model.PointManager;
import ar.edu.unicen.pladema.ui.mywidgets.RectSize;
import ar.edu.unicen.pladema.ui.mywidgets.SizePanel;
import ar.edu.unicen.pladema.ui.panel.AbstractPanel;
import ar.edu.unicen.pladema.vc1.model.GraphicAttributes;

public class POP2E1 extends AbstractPanel implements Observer {
	private Label opciones;
	private SizePanel sizePanel;
	private RectSize rectSize;
	private Button recargar;
	private Button random;
	private IPaintIt ipi;
	private PointManager pointManager;
	private GraphicAttributes graphicAttributes;
	
	public POP2E1(Composite parent, IPaintIt pi, PointManager pm, GraphicAttributes ga) {
		super(parent);
		this.ipi = pi;
		this.pointManager = pm;
		this.graphicAttributes = ga;
		ga.addObserver(this);
	}

	@Override
	protected void createWidgets() {
		opciones = new Label(this, SWT.NONE);
		opciones.setText("OPCIONES");
		Font f = new Font(this.getDisplay(), "Arial", 24, SWT.BOLD);
		opciones.setFont(f);
		sizePanel = new SizePanel(this);
		
		
		rectSize = new RectSize(this);
		recargar = SWTUtils.createButton(this, "DRAW", new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				graphicAttributes
					.add(GraphicAttributes.CLIENT_WIDTH, rectSize.getWidth())
					.add(GraphicAttributes.CLIENT_HEIGHT, rectSize.getHeight());
				ipi.doPaint();
			}
		});
		random = SWTUtils.createButton(this, "RANDOM_POINTS", new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				pointManager.doRandom(
						graphicAttributes.getInt(GraphicAttributes.CLIENT_WIDTH), 
						graphicAttributes.getInt(GraphicAttributes.CLIENT_HEIGHT));
				ipi.doPaint();
			}
		});
	}

	@Override
	protected void layoutWidgets() {
		setLayout(new FormLayout());
		
		opciones.setLayoutData(SWTUtils.createTopFormData(5));
	
		sizePanel.setLayoutData(SWTUtils.createBelowFormData(opciones, 5));
		
		rectSize.setLayoutData(SWTUtils.createBelowFormData(sizePanel,5));
		random.setLayoutData(SWTUtils.createBelowFormData(rectSize,5));
		recargar.setLayoutData(SWTUtils.createBelowFormData(random,5));
	}

	private void loadGraphicData() {
		sizePanel.setValue(
				graphicAttributes.get(GraphicAttributes.SIZE_WIDTH), 
				graphicAttributes.get(GraphicAttributes.SIZE_HEIGHT));
		rectSize.setWidth(graphicAttributes.getInt(GraphicAttributes.CLIENT_WIDTH));
		rectSize.setHeight(graphicAttributes.getInt(GraphicAttributes.CLIENT_HEIGHT));
	}
	@Override
	public void update(Observable o, Object arg) {
		if (graphicAttributes.equals(o)) {
			loadGraphicData();
		}
	}

	public void setPointManager(PointManager pointManager) {
		this.pointManager = pointManager;
	}

}
